package canglang.monitor.login.action;

import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.user.model.UserModel;
import canglang.monitor.user.service.UserMgr;

public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 6872685508877439264L;
	
	private UserModel userModel;
	
	private UserMgr userMgr;
	
	public UserModel getUserModel() {
		return userModel;
	}
	
	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

	public UserMgr getUserMgr() {
		return userMgr;
	}

	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}

	public String login(){
		UserModel user = userMgr.getUser(userModel.getUserId());
	    if(user==null){
	        this.addActionError(AppDefaultProperty.getProperty("label.user.does.not.exist"));
	        return INPUT;
	    }
	    if(!user.getUserPassword().equals(userModel.getUserPassword())){
	        this.addActionError(AppDefaultProperty.getProperty("label.password.wrong"));
	        return INPUT;
	    }
	    this.getSession().setAttribute("user", user);
        return SUCCESS;

	}

	public String changePassword(){
        UserModel user = userMgr.getUser(userModel.getUserId());
        if(user!=null){
            if(user.getUserPassword().equals(userModel.getUserPassword())){
                user.setUserPassword(userModel.getNewPassword());
                userMgr.saveUser(user);
            }else{
                this.addActionError(AppDefaultProperty.getProperty("label.password.wrong"));
                return INPUT;
            }
        }else{
            this.addActionError(AppDefaultProperty.getProperty("label.user.does.not.exist"));
            return INPUT;
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.password.change.successful"));
        return SUCCESS;
    }
}
